package com.huishen.edrivenew.widget;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.huishen.edrivenew.R;
import com.huishen.edrivenew.bean.DateBean;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;

/**
 * 日历控件
 */
public class CalendarFragment extends Fragment {

    private FragmentActivity activity;
    private TextView title;
    private ImageButton premouth, nextmouth;
    private ViewPager mPager;


    public static CalendarFragment create() {
        CalendarFragment fragment = new CalendarFragment();
        return fragment;
    }
    
    public static CalendarFragment create(DateBean[] days) {
        CalendarFragment fragment = new CalendarFragment();
        return fragment;
    }

    public static CalendarFragment create(boolean isSection, String beginDate, String endDate,
            int[] colors) {

        CalendarFragment fragment = new CalendarFragment();
        Bundle args = new Bundle();
        args.putBoolean("isSection", isSection);
        args.putString("beginDate", beginDate);
        args.putString("endDate", endDate);
        args.putIntArray("colors", colors);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            this.activity = (FragmentActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + "must implement FragmentActivity");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.calendar, null);
        registView(rootView);
        init();
        return rootView;
    }

    /**
     * 注册组件
     */
    private void registView(View rootView) {
        mPager = (ViewPager) rootView.findViewById(R.id.calendarpager);
        title = (TextView) rootView.findViewById(R.id.calendar_tv_mouth);
        this.premouth = (ImageButton) rootView.findViewById(R.id.calendar_btn_premouth);
        this.nextmouth = (ImageButton) rootView.findViewById(R.id.calendar_btn_nextmouth);
    }

    
    /**
     * 初始化事件
     */
    private void init() {
        
        CalendarPagerAdapter mPagerAdapter =
                new CalendarPagerAdapter(this.activity.getSupportFragmentManager());

//        /************** 选择区间设置 *********************/
//        if (this.isSection) {
//            mPagerAdapter.setSection(beginDate, endDate, this.colors);
//        }
        
        /************** 选择区间设置结束 ******************/
        mPager.setAdapter(mPagerAdapter);
        
        mPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageSelected(int index) {
            }
        });
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
        String d = df.format(new Date());
        title.setText(d);
        
//        setOnClick();
    }
    
    private void setOnClick() {
        this.premouth.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mPager.getCurrentItem() > 0) {
                    mPager.setCurrentItem(mPager.getCurrentItem() - 1);
                } else {
                    Toast.makeText(activity, "对不起，已经是第一页了", Toast.LENGTH_SHORT).show();
                }
            }
        });
        this.nextmouth.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mPager.getCurrentItem() != mPager.getChildCount() - 1) {
                    mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                } else {
                    Toast.makeText(activity, "对不起，已经是最后一页了", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    
    
    
    
}
